import { createStore } from "vuex"
import cache from "./cache"
import ajax from "./ajax"
import defaultConfig from "./defaultConfig";
import { ElMessageBox } from "element-plus";
import { Arraysort, deepMerge } from "./tools";
import emitter from "./emitter";
import deepmerge from "deepmerge";
const configCache = cache.get("config", {});
let lastTime = null;
const store = createStore({
  state: {
    tabbar: cache.get("tabbar", []),
    link: cache.get("link", []),
    isLogin: Boolean(cache.get("token", false)),
    editStatus: false,
    config: Object.assign({}, deepMerge(defaultConfig, configCache)),
    site: {
      upload: 'http://127.0.0.1/upload',
      email: ''
    }
  },
  mutations: {
    updateLink(state, list = false) {
      if (lastTime != null) {
        clearTimeout(lastTime)
      }
      lastTime = setTimeout(() => {
        if (!list) {
          list = state.link;
        }
        cache.set("link", list)
        state.link = deepMerge([], list);
        if (cache.get("token", false)) {
          ajax({
            url: '/link/update',
            method: 'post',
            data: {
              link: list
            }
          }).then(() => { }).catch(() => { })
        }
        emitter.emit("RefreshLink", list)
        lastTime = null
      }, 300);
    },
    updateConfig(state) {
      cache.set("config", state.config)
      if (cache.get("token", false)) {
        ajax({
          url: '/config/update',
          method: 'post',
          data: {
            config: state.config
          }
        }).then(el => {
        })
      }
    },
    updateTabbar(state, list) {
      console.log(list);
      state.tabbar = list;
      cache.set("tabbar", list)
      if (cache.get("token", false)) {
        ajax({
          url: '/tabbar/update',
          method: 'post',
          data: {
            tabbar: list
          }
        }).then(el => {
        })
      }
    },
    //获取站点配置文件
    pullSiteConfig(state) {
      ajax({
        url: '/api/site',
      }).then(el => {
        if (el.code == 1) {
          state.site = Object.assign(state.site, el.data)
        }
      })
    },
    pullFoooter(state) {
      if (!cache.get("token", false)) {
        if (cache.get('tabbar', false)) {
          return;
        }
      }
      ajax({
        url: '/tabbar/get',
      }).then(el => {
        if (el.code == 1) {
          state.tabbar = el.data
          cache.set("tabbar", el.data)
        }
      })
    },
    pullConfig(state) {
      if (!cache.get("token", false)) {
        if (cache.get('config', false)) {
          return;
        }
      }
      ajax({
        url: '/config/get',
      }).then(el => {
        if (el.code == 1) {
          state.config = Object.assign({}, deepMerge(state.config, el.data));
          cache.set("config", state.config)
        } else {
          state.config = Object.assign({}, defaultConfig)
        }
      })
    },
    pullLink(state) {
      if (!cache.get("token", false)) {
        if (cache.get('link', false)) {
          return;
        }
      }
      ajax({
        url: '/link/get',
      }).then(el => {
        if (el.code == 1) {
          let tips = 0;
          let a = [];
          el.data.forEach(ell => {
            if (ell?.id) {
              a.push(ell)
            } else {
              tips = 1
            }
          })
          if (state.link != a) {
            state.link = a
          }
          cache.set("link", a)
          if (tips == 1) {
            store.commit('updateLink', state.link)
          }
        }
      })
    },
    //登录状态修改
    loginIn(state) {
      state.isLogin = true;
    },
    //退出登录
    loginOut(state) {
      state.isLogin = false;
      cache.del("token").del("id");
      ElMessageBox.confirm("是否删除本地缓存数据？", {
        confirmButtonText: '删除',
        cancelButtonText: '保留'
      }).then(el => {
        cache.del("config").del("tabbar").del('link').del("noteList")
        store.commit("pullFoooter")
        store.commit("pullLink")
        emitter.emit("reload")
      }).catch(() => {

      })
    },
    delLink(state, id) {
      const index = state.link.findIndex(el => el.id == id);
      state.link.splice(index, 1);
      store.commit("updateLink");
    },
    setLink(state, data) {
      const index = state.link.findIndex(el => el.id == data.id);
      state.link[index] = data;
      store.commit("updateLink");
    },
    setLinkSort(state, datas) {
      const index = state.link.find(el => el.id == datas.id);
      index.sort = datas.sort
      index.pid = datas.pid
      store.commit("updateLink");
    },
    setPid(state, datas) {
      const index = state.link.findIndex(el => el.id == datas.id);
      state.link[index].pid = datas.pid
      store.commit("updateLink");
    },
    updateDiffLink(state, list) {
      let l = deepMerge([], state.link);
      list.forEach(el => {
        let y = l.find(ll => ll.id == el.id)
        if (y) {
          y['sort'] = el?.sort;
          y['pid'] = el?.pid
        }
      })
      cache.set("link", l)
      if (cache.get("token", false)) {
        ajax({
          url: '/link/update',
          method: 'post',
          data: {
            link: l
          }
        }).then(() => { }).catch(() => { })
      }
      state.link = l;
    }
  },
  actions: {
    loginIn({ commit }) {
      commit("loginIn")
      commit("pullLink")
      commit("pullFoooter")
      commit("pullConfig")
    }
  },
  getters: {
    //返回列表
    link(state) {
      return state.link
    },
    config(state) {
      return state.config
    },
    findChildren: (state) => (id) => {
      if (id) {
        return state.link.filter(ell => {
          if (ell.pid == id) {
            return true;
          }
        })
      }
      return [];
    },
    //根据图标Id查询图标的具体信息
    find: (state) => (id) => {
      let t = state.link.find(el => el.id == id)
      if (t) {
        return Object.assign({}, t);
      }
      return false
    }
  }
})
export default store